package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
)

type KechengsRBody struct {
	Data       []models.Kecheng `json:"data"`
	PageNo     int              `json:"pageNo"`
	PageSize   int              `json:"pageSize"`
	TotalCount int64            `json:"totalCount"`
	TotalPage  int64            `json:"totalPage"`
}
type JiaochengsRBody struct {
	Data       []models.Jiaocheng `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}
type JiaochengsBody struct {
	PageNo      int   `json:"pageNo"`
	Pagesize    int   `json:"pagesize"`
	Hongniangid int64 `json:"hongniangid" gorm:"hongniangid"`
	Kechengid   int64 `json:"kechengid"`
}

func Jiaochengs(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Jiaocheng
	res := data_source.Db
	res.Model(&models.Jiaocheng{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := JiaochengsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
func Kechengs(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Kecheng
	res := data_source.Db
	res.Model(&models.Kecheng{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var kechengidarr []int64
	for _, v := range users {
		kechengidarr = append(kechengidarr, v.ID)
	}
	var baoming []models.Kechengbm
	data_source.Db.Where("kechengid in (?)", kechengidarr).Where("hongniangid = ?", hongniangid).Find(&baoming)
	for k, v := range users {
		users[k].Baoming = 1
		for _, b := range baoming {
			if v.ID == b.Kechengid {
				users[k].Baoming = 2
			}
		}
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := KechengsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
func Kechengbm(ctx *gin.Context) {
	request := JiaochengsBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	kechengid := request.Kechengid
	var baoming models.Kechengbm
	data_source.Db.Where("kechengid = ?", kechengid).Where("hongniangid = ?", hongniangid).First(&baoming)
	if baoming.ID == 0 {
		bm := models.Kechengbm{
			Kechengid:   kechengid,
			Hongniangid: hongniangid,
		}
		data_source.Db.Create(&bm)
	} else {
		data_source.Db.Where("id = ?", baoming.ID).Delete(&models.Kechengbm{})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
